package hupuCBA

import (
	"cd-crawler/types"
	"github.com/PuerkitoBio/goquery"
	"log"
	"regexp"
	"strconv"
)

var idRe = regexp.MustCompile(`https://cba.hupu.com/players/(.*).html`)

var roleRe = regexp.MustCompile(`<strong>位置: </strong><a href="[^>]*>(.*)</a>\((.*)号\)<br/>`)
var heightRe = regexp.MustCompile(`<strong>身高: </strong>([^<]*)<br/>`)
var weightRe = regexp.MustCompile(`<strong>体重: </strong>([^<]*)<br/>`)
var birthdayRe = regexp.MustCompile(`<strong>生日: </strong>([^<]*)`)
var teamRe = regexp.MustCompile(`<strong>球队:</strong><a href="[^>]*>(.*)</a><br/>`)
var nationRe = regexp.MustCompile(`<strong>民族:</strong>([^<]*)<br/>`)
var schoolRe = regexp.MustCompile(`<strong>学校: </strong>([^<]*)<br/>`)
var nativeRe = regexp.MustCompile(`<strong>籍贯: </strong>([^<]*)`)

// 返回值要有url，解析的方法，需要的值
func ParsePlayerDetail(params types.ParseParams) types.ParseResult {
	////下载内容
	//resp, err := http.Get(url)
	//if err != nil {
	//	log.Printf("http Get occur error: %s", err)
	//	return types.ParseResult{}
	//}
	//defer resp.Body.Close()
	//
	//if resp.StatusCode != 200 {
	//	log.Printf("status code error: %d %s", resp.StatusCode, resp.Status)
	//}
	//reader, err := tools.DecodeHTMLBody(resp.Body, "")

	parseResult := types.ParseResult{}

	tempId := getStrWithRe(params.Url, idRe)
	if tempId == "" {
		return parseResult
	}

	// 解析内容
	// Load the HTML document
	doc, err := goquery.NewDocumentFromReader(params.Reader)

	if err != nil {
		log.Printf("error occur goquery.NewDocumentFromReader: %s", err)
	}

	player := Player{}

	sTable := doc.Find("table:nth-child(1)")
	player.Name = sTable.Find("tr:nth-child(1) td:nth-child(2) strong").Text()
	tempDiv1, err := sTable.Find("tr:nth-child(2) td:nth-child(1)").Html()
	if err != nil {
		log.Printf("error occur selection.Html(): %s", err)
	}
	//fmt.Println(tempDiv1)
	match := roleRe.FindSubmatch([]byte(tempDiv1))
	if len(match) > 2 {
		player.Role = string(match[1])
		num, _ := strconv.Atoi(string(match[2]))
		player.Number = num
	}

	player.Height = getStrWithRe(tempDiv1, heightRe)

	player.Weight = getStrWithRe(tempDiv1, weightRe)

	player.Birthday = getStrWithRe(tempDiv1, birthdayRe)

	tempDiv2, err := sTable.Find("tr:nth-child(2) td:nth-child(2)").Html()
	//fmt.Println(tempDiv2)
	if err != nil {
		log.Printf("error occur selection.Html(): %s", err)
	}
	player.Team = getStrWithRe(tempDiv2, teamRe)

	player.Nation = getStrWithRe(tempDiv2, nationRe)

	player.School = getStrWithRe(tempDiv2, schoolRe)

	player.Native = getStrWithRe(tempDiv2, nativeRe)

	sTd := sTable.Find("tr:nth-child(2) td:nth-child(3) div:nth-child(2)")
	tempScore, err := strconv.ParseFloat(sTd.Find(".playtable:nth-child(1)").Text(), 64)
	if err != nil {
		tempScore = 0
	}
	player.Score = tempScore

	tempRebound, err := strconv.ParseFloat(sTd.Find(".playtable:nth-child(2)").Text(), 64)
	if err != nil {
		tempRebound = 0
	}
	player.Rebound = tempRebound

	tempBlock, err := strconv.ParseFloat(sTd.Find(".playtable:nth-child(3)").Text(), 64)
	if err != nil {
		tempBlock = 0
	}
	player.Block = tempBlock

	item := types.Item{
		Url:     params.Url,
		Id:      tempId,
		Payload: player,
	}

	parseResult.Item = append(parseResult.Item, item)

	//log.Printf("%+v", item)

	return parseResult
}

func getStrWithRe(content string, re *regexp.Regexp) string {
	match := re.FindSubmatch([]byte(content))
	if len(match) > 1 {
		return string(match[1])
	}
	return ""
}
